package com.zhiyou100.util;

import com.zhiyou100.entity.Exam;
import com.zhiyou100.entity.Student;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;

/**
 * @author yang
 * @className ExamUtil
 * @packageName com.zhiyou100.util
 * @Description TODO 成绩工具类
 * @date 2020-08-26 20:28
 */
public class ExamUtil {


    /**
     * 把 exam 写入指定的位置中
     *
     * @param e    考试
     * @param path 路径
     * @throws Exception 异常
     */
    public static void creatText(Exam e, String path) throws Exception {
        // 文件名字和考试名字相同
        final File file = new File(path, e.getEname() + "结果.txt");

        try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file))) {

            bufferedWriter.write("名次\t名字\t分数");
            bufferedWriter.newLine();
            bufferedWriter.flush();
            for (int i = 0; i < e.getStus().size(); i++) {
                final Student s = e.getStus().get(i);
                String line = i + "\t" + s.getSname() + "\t" + s.getScore();
                bufferedWriter.write(line);
                bufferedWriter.newLine();
                bufferedWriter.flush();

            }
        }

    }
}
